/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kosongkosong.scheduler;

import com.kosongkosong.model.Story;
import com.kosongkosong.service.DataService;
import com.kosongkosong.utils.UrlUtils;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import org.cyberneko.html.parsers.DOMParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import static com.kosongkosong.scheduler.UrlConstants.*;
/**
 *
 * @author ifnu
 */
@Controller
public class NewsParserScheduler {

    @Autowired private DataService dataService;

    private final Set<String> headerToIgnore = new HashSet<String>();

    public NewsParserScheduler() {
        headerToIgnore.add("SCORES & FIXTURES");
        headerToIgnore.add("TABLES");
        headerToIgnore.add("TEAMS");
        headerToIgnore.add("STATCENTRE");
    }

    @RequestMapping(value="/parse-news",method=RequestMethod.GET)
    public void parseNews(HttpServletResponse response) throws Exception{
        //fetch url
        String newsString = UrlUtils.fetchUrl(WC_2010_NEWS);
        List<Story> stories = parseStories(newsString);
        List<Story> savedStories = new ArrayList<Story>();
        for (Story s : stories) {
            Story persistenceStory = dataService.getStory(s.getId());
            if(persistenceStory==null){
                //ambil image
                //ambil full story
                String fullStory = UrlUtils.fetchUrl(s.getStoryUrl());
                //parse isi story
                parseCompleteStory(s, fullStory);
                s.setHeadline(Boolean.FALSE);
                savedStories.add(s);
                //update last headline
            } else {
                //do logging
            }
        }
        newsString = UrlUtils.fetchUrl(WC_2010_MORE_NEWS);
        stories = parseStories(newsString);
        for (Story s : stories) {
            Story persistenceStory = dataService.getStory(s.getId());
            if(persistenceStory==null){
                //ambil image
                //ambil full story
                String fullStory = UrlUtils.fetchUrl(s.getStoryUrl());
                //parse isi story
                parseCompleteStory(s, fullStory);
                s.setHeadline(Boolean.FALSE);
                savedStories.add(s);
                //update last headline
            } else {
                //do logging
            }
        }
        dataService.save(savedStories);
        response.getOutputStream().close();
    }

    String getStoryId(String href){
        int storyIdIndex = href.indexOf("=");
        int ampIndex = href.indexOf("&");
        return href.substring(storyIdIndex+1,ampIndex);
    }

    List<Story> parseStories(String newsString){
        try{
            DOMParser parser = new DOMParser();
            parser.parse(new InputSource(new StringReader(newsString)));
            //get main page URL
            Document d  = parser.getDocument();
            NodeList nodeList = d.getElementsByTagName("div");
            List<Story> stories = new ArrayList<Story>();
            for(int i=0;i<nodeList.getLength();i++){
                Node node = nodeList.item(i);
                NamedNodeMap map = node.getAttributes();
                if(map!=null){
                    Node classAttribute = map.getNamedItem("class");
                    if(classAttribute!=null){
                        String classAttributeValue = classAttribute.getNodeValue();
                         if(classAttributeValue!=null && 
                            (classAttributeValue.equals("ind") || classAttributeValue.equals("ind alt") || classAttributeValue.equals("ind tL"))){
                            NodeList subHeadChilds = node.getChildNodes();
                            if(subHeadChilds.getLength()==1){
                                Node subHeadA = subHeadChilds.item(0);
                                //ignore unwanted link
                                if(!headerToIgnore.contains(subHeadA.getTextContent().trim())){
                                    Story story = new Story();
                                    story.setSubHeader(subHeadA.getTextContent());
                                    NamedNodeMap subHeadMap = subHeadA.getAttributes();
                                    String href = subHeadMap.getNamedItem("href").getNodeValue();
                                    story.setStoryUrl(SOCCERNET + href);
                                    //parse story href
                                    story.setId(getStoryId(href));
                                    //cek apakah story ini udah ada di dalam datastore
                                    stories.add(story);
                                }
                            }
                        } 
                    }
                }
            }
            return stories;
        } catch (SAXException ex) {
            Logger.getLogger(HeadlineParserScheduler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HeadlineParserScheduler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    Story parseCompleteStory(Story story, String html){
        try {
            DOMParser parser = new DOMParser();
            parser.parse(new InputSource(new StringReader(html)));
            //get main page URL
            Document d = parser.getDocument();
            NodeList nodeList = d.getElementsByTagName("div");

            for(int i=0;i<nodeList.getLength();i++){
                Node node = nodeList.item(i);
                NamedNodeMap map = node.getAttributes();
                if(map!=null){
                    Node classAttribute = map.getNamedItem("class");
                    if(classAttribute!=null){
                        String classAttributeValue = classAttribute.getNodeValue();
                         if(classAttributeValue!=null && classAttributeValue.equals("ind story")){
                            story.setFullStory(node.getTextContent());
                            break;
                         }
                    }
                }
            }

        } catch (SAXException ex) {
            Logger.getLogger(HeadlineParserScheduler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HeadlineParserScheduler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return story;
    }
}
